package com.cindata

import com.cindata.dictionary.*
import grails.gorm.transactions.Transactional
import org.springframework.security.access.annotation.Secured

import static org.springframework.http.HttpStatus.*

@Secured(['isAuthenticated()'])
@Transactional
class MortgageCollectionRecordController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond MortgageCollectionRecord.list(params), model:[mortgageCollectionRecordCount: MortgageCollectionRecord.count()]
    }

    def show(MortgageCollectionRecord mortgageCollectionRecord) {
        def riskWarning = RiskWarning.findById(Long.parseLong(params['riskWarning']))
        respond mortgageCollectionRecord,model: [riskWarning: riskWarning]
    }
    def abnormalMortgageBatchShow(MortgageCollectionRecord mortgageCollectionRecord) {
        def abnormalMortgageBatch = AbnormalMortgageBatch.findById(Long.parseLong(params['abnormalMortgageBatch']))
        respond mortgageCollectionRecord,model: [abnormalMortgageBatch: abnormalMortgageBatch]
    }
    def create()
    {
        def riskWarning = RiskWarning.findById(Long.parseLong(params['id']))
        def targetUri = request.getHeader("referer")
        params['targetUri'] = targetUri
        respond new MortgageCollectionRecord(params), model: [riskWarning: riskWarning]
    }

    def create1()
    {
        def abnormalMortgageBatch = AbnormalMortgageBatch.findById(Long.parseLong(params['id']))
        def targetUri = request.getHeader("referer")
        params['targetUri'] = targetUri
        respond new MortgageCollectionRecord(params), model: [abnormalMortgageBatch: abnormalMortgageBatch]
    }

    @Transactional
    def save(MortgageCollectionRecord mortgageCollectionRecord)
    {
        if (mortgageCollectionRecord == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (mortgageCollectionRecord.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond mortgageCollectionRecord.errors, view:'create'
            return
        }

        def record = MortgageCollectionRecord.findByMortgage(mortgageCollectionRecord?.mortgage)
        if (!record)
        {
            println params['riskWarning']

            def riskWarning = RiskWarning.findById(params['riskWarning'])
            riskWarning.actualStartTime = new Date()
            riskWarning.save flush: true

            if(record?.agencyAccount ){
                def temp =  BatchAccount.findByAbnormalMortgageBatchAndAccount(record?.abnormalMortgageBatch,record?.agencyAccount)
                if(!temp){
                    BatchAccount batchAccount = new BatchAccount()
                    batchAccount.abnormalMortgageBatch = record.abnormalMortgageBatch
                    batchAccount.account = record.agencyAccount
                    batchAccount.save(flush:true)
                }
            }
        }

        mortgageCollectionRecord.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'mortgageCollectionRecord.label', default: 'MortgageCollectionRecord'), mortgageCollectionRecord.id])
                redirect url: params['targetUri']
            }
            '*' { respond mortgageCollectionRecord, [status: CREATED] }
        }
    }

    @Transactional
    def save1(MortgageCollectionRecord mortgageCollectionRecord)
    {
        if (mortgageCollectionRecord == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (mortgageCollectionRecord.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond mortgageCollectionRecord.errors, view:'create'
            return
        }

        def record = MortgageCollectionRecord.findByAbnormalMortgageBatch(mortgageCollectionRecord?.abnormalMortgageBatch)
        if (!record)
        {
            println params['abnormalMortgageBatch']

            def abnormalMortgageBatch = AbnormalMortgageBatch.findById(params['abnormalMortgageBatch'])
            abnormalMortgageBatch.actualStartTime = new Date()
            abnormalMortgageBatch.save flush: true

            if(record?.agencyAccount ){
                def temp =  BatchAccount.findByAbnormalMortgageBatchAndAccount(record?.abnormalMortgageBatch,record?.agencyAccount)
                if(!temp){
                    BatchAccount batchAccount = new BatchAccount()
                    batchAccount.abnormalMortgageBatch = record.abnormalMortgageBatch
                    batchAccount.account = record.agencyAccount
                    batchAccount.save(flush:true)
                }
            }
        }

        mortgageCollectionRecord.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'mortgageCollectionRecord.label', default: 'MortgageCollectionRecord'), mortgageCollectionRecord.id])
                redirect url: params['targetUri']
            }
            '*' { respond mortgageCollectionRecord, [status: CREATED] }
        }
    }

    def edit(MortgageCollectionRecord mortgageCollectionRecord)
    {
        def riskWarning = RiskWarning.findById(Long.parseLong(params['riskWarning']))
        def targetUri = request.getHeader("referer")
        params['targetUri'] = targetUri
        respond mortgageCollectionRecord,model: [riskWarning: riskWarning]
    }

    def edit1(MortgageCollectionRecord mortgageCollectionRecord)
    {
        def abnormalMortgageBatch = AbnormalMortgageBatch.findById(Long.parseLong(params['abnormalMortgageBatch']))
        def targetUri = request.getHeader("referer")
        params['targetUri'] = targetUri
        respond mortgageCollectionRecord,model: [abnormalMortgageBatch: abnormalMortgageBatch]
    }

    @Transactional
    def update(MortgageCollectionRecord mortgageCollectionRecord) {
        if (mortgageCollectionRecord == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (mortgageCollectionRecord.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond mortgageCollectionRecord.errors, view:'edit'
            return
        }

        if(mortgageCollectionRecord?.agencyAccount ){
            //存在旧数据
            if(params['agencyAccount1']){
                def temp = BatchAccount.findByAbnormalMortgageBatchAndAccount(mortgageCollectionRecord?.abnormalMortgageBatch,Account.findById(Long.parseLong(params['agencyAccount1'])))
                temp.delete(flush:true)
            }
            def temp =  BatchAccount.findByAbnormalMortgageBatchAndAccount(mortgageCollectionRecord?.abnormalMortgageBatch,mortgageCollectionRecord?.agencyAccount)
            if(!temp){
                BatchAccount batchAccount = new BatchAccount()
                batchAccount.abnormalMortgageBatch = mortgageCollectionRecord.abnormalMortgageBatch
                batchAccount.account = mortgageCollectionRecord.agencyAccount
                batchAccount.save(flush:true)
            }
        }
        mortgageCollectionRecord.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'mortgageCollectionRecord.label', default: 'MortgageCollectionRecord'), mortgageCollectionRecord.id])
                redirect url: params['targetUri']
            }
            '*'{ respond mortgageCollectionRecord, [status: OK] }
        }
    }

    @Transactional
    def delete(MortgageCollectionRecord mortgageCollectionRecord) {

        if (mortgageCollectionRecord == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        mortgageCollectionRecord.delete flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'mortgageCollectionRecord.label', default: 'MortgageCollectionRecord'), mortgageCollectionRecord.id])
                redirect action:"index", method:"GET"
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'mortgageCollectionRecord.label', default: 'MortgageCollectionRecord'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}
